<?php
defined('_JEXEC') or die('Restricted access');
//JToolBarHelper::title(JText::_('NS Pro Newsletters'), 'inbox.png');

$db =& JFactory::getDBO();
$db->setQuery('SELECT `id`, `lname` FROM `#__nspro_lists`');
$mlists = $db->loadObjectList();

$db->setQuery('SELECT * FROM `#__nspro_html_templates`');
$templates = $db->loadObjectList();

$mattachments = array();
if (!($this->isNew)) {
  $query = 'SELECT * FROM `#__nspro_newsletter_attachments` WHERE `newsletter_id` = "' . mysql_escape_string($this->newsletter->id) . '" ORDER BY `id`';
  $db->setQuery($query);

  $mattachments = $db->loadObjectList();
}

$jversion = new JVersion();
$j15 = ($jversion->RELEASE == '1.5');
//$j16 = ($jversion->RELEASE == '1.6');

jimport('joomla.application.component.helper');
$params = JComponentHelper::getParams( 'com_nspro' );
if ($j15 == true) {
  $component = JComponentHelper::getComponent( 'com_nspro' );
  $params = new JParameter( $component->params );
}

$upfilesize = $params->get('upfilesize', '1000000');

$visibility = (($this->newsletter->is_scheduled == '0') ? "visibility:hidden; position:absolute" : "position: relative; visibility: visible;");

$editor =& JFactory::getEditor();
?>
<style type="text/css">
<!--

table.stexts td.stkey {
  color: #666;
  font-weight: bold;
  border-bottom: 1px solid #D5D5D5;
  border-right: 1px solid #D5D5D5;
  background-color: #f9f9f9;
}

-->
</style>
<script type="text/javascript">
<!--

  function schedulechange(val) {
    if (val == '1') {
      document.getElementById('scheduledatetimetr').setAttribute('style', 'position: relative; visibility: visible;');
    }
    else if (val == '0') {
      document.getElementById('scheduledatetimetr').setAttribute('style', 'position: absolute; visibility: hidden;');
    }
  }

  function useTemplate() {
    var text = <?php echo $editor->getContent( 'message' ); ?>
    if (text != "") {
      doaddtemplate = confirm("<?php print JText::_('There is already text in your Newsletter Message. Are you sure you want to apply the selected template?') ?>");
      if (doaddtemplate == true) {
        doadd = true;
      }
    }
    else {
      doadd = true;
    }

    if (doadd == true) {
      var myvalue = -1;
      for (var i=0; i < document.adminForm.templatetoapply.length; i++) {
        if (document.adminForm.templatetoapply[i].checked) {
          var myvalue = document.adminForm.templatetoapply[i].value;
        }
      }
      if (myvalue == -1) {
        var myvalue = document.adminForm.templatetoapply.value;
      }
      templateinput = document.createElement("input");
      templateinput.setAttribute("type", "hidden");
      templateinput.setAttribute("name", "template_id");
      templateinput.setAttribute("value", myvalue);
      document.templateform.appendChild(templateinput);
      document.templateform.submit();
    }

  }

  <?php if ($j15 == true) { ?>
  function submitbutton(task) {
  <?php } else { ?>
  Joomla.submitbutton = function(task) {
  <?php } ?>
    var form = document.adminForm;

    if (task == 'ncancel') {
      <?php if ($j15 != true) { ?>Joomla.<?php } ?>submitform( task );
      return;
    }

    var myemailspersend = document.getElementById('emails_per_send').value;

    if ( isNaN( parseInt(myemailspersend) ) ) {
      alert("'"+myemailspersend+"' is not a valid 'Emails Per Send' value.");
      return;
    }

    selRef = document.getElementById('mailing_lists2');
    var mstring = '';
    for (var i=0; i < selRef.options.length;i++) {
      if (selRef.options[i].selected) {
        if (mstring == '') {
          mstring = selRef.options[i].value;
        }
        else  {
          mstring = mstring + ',' + selRef.options[i].value;
        }
      }
    }

    document.getElementById('mailing_lists').value = mstring;

    // do field validation
    var text = <?php echo $editor->getContent( 'message' ); ?>
    if (text == ""){
      alert( "<?php echo JText::_( 'Newsletter must have some text', true ); ?>" );
    } else {
      <?php
      echo $editor->save( 'message' );
      ?>
      <?php if ($j15 != true) { ?>Joomla.<?php } ?>submitform( task );
    }

  }

// -->
</script>
<form id="templateform" name="templateform" method="post" action="index.php">
<input type="hidden" name="option" value="com_nspro"/>
<input type="hidden" name="task" value="applyTemplate"/>
<input type="hidden" name="controller" value="nspro" />
<input type="hidden" name="nid" value="<?php print $this->newsletter->id; ?>"/>
</form>
<table>
<tr><td>
<form action="index.php" method="post" name="adminForm" id="adminForm">
<div class="col width-10 <?php if ($j15 != true) { print 'fltlft'; } ?>">
    <fieldset class="adminform">
        <legend><?php echo JText::_( 'Details' ); ?></legend>
          <input type="hidden" name="datetime" value="<?php echo $this->newsletter->datetime; ?>"/>
          <input type="hidden" name="sent" value="<?php echo $this->newsletter->sent; ?>"/>
          <input type="hidden" name="LastID" value="<?php echo $this->newsletter->LastID; ?>"/>
        <table class="admintable  <?php if ($j15 != true) { print 'adminform'; } ?>">
        <tr>
            <td width="100" class="key">
                <label for="fName">
                    <?php echo JText::_( 'From Name' ); ?>:
                </label>
            </td>
            <td>
                <input class="text_area" type="text" name="fName" id="fname" size="180" maxlength="250"
                value="<?php echo $this->newsletter->fName;?>" />
            </td>
        </tr>
        <tr>
            <td width="110" class="key">
                <label for="fEmail">
                    <?php echo JText::_( 'From Email' ); ?>:
                </label>
            </td>
            <td>
                <input class="inputbox" type="text" name="fEmail" id="femail" size="180" value="<?php echo $this->newsletter->fEmail; ?>" />
            </td>
        </tr>
        <tr>
            <td width="110" class="key">
                <label for="subject">
                    <?php echo JText::_( 'Subject' ); ?>:
                </label>
            </td>
            <td>
                <input class="inputbox" type="text" name="subject" id="subject" size="180" value="<?php echo $this->newsletter->subject; ?>" />
            </td>
        </tr>
        <tr>
            <td width="110" class="key">
                <label for="mailing_lists">
                    <?php echo JText::_( 'Mailing Lists' ); ?>:
                </label>
            </td>
            <td>
                <input type="hidden" name="mailing_lists" id="mailing_lists" value="<?php echo $this->newsletter->mailing_lists; ?>" />
                <select class="inputbox" size="5" style="width: 258px;" multiple="multiple" name="mailing_lists2[]" id="mailing_lists2">
                <?php
                foreach($mlists as $mlist) {
                  print '<option value="' . $mlist->id . '" id="list_' . $mlist->id . '"';
                  print '>' . $mlist->lname . '</option>';
                }
                ?>
                </select>
            </td>
        </tr>
        <tr>
            <td width="110" class="key">
                <label for="is_scheduled">
                    <?php echo JText::_( 'Scheduled Sending' ); ?>:
                </label>
            </td>
            <td>
              <?php
							$schedules = array();

							$sc1 = new stdClass();
							$sc1->value = '0';
							$sc1->text = JText::_('No Schedule');
							$schedules[] = $sc1;

							$sc1 = new stdClass();
							$sc1->value = '1';
							$sc1->text = JText::_('Scheduled To Send');
							$schedules[] = $sc1;

							echo JHTML::_( 'select.genericlist', $schedules, 'is_scheduled', 'class="inputbox" style="width: 250px;" onChange="javascript:schedulechange(this.value);"', 'value', 'text', $this->newsletter->is_scheduled );
              ?>
            </td>
        </tr>
        <tr id="scheduledatetimetr" style="<?php print $visibility; ?>">
            <td width="110" class="key">
                <label for="scheduledate">
                    <?php echo JText::_( 'Schedule Date and Time' ); ?>:
                </label>
            </td>
            <td>
              <input class="inputbox" type="text" name="scheduledatetime" id="scheduledatetime" size="180" value="<?php echo $this->newsletter->scheduledatetime; ?>" />
            </td>
        </tr>          
        <tr>
            <td width="110" class="key">
                <label for="emails_per_send">
                    <?php echo JText::_( 'Emails Per Send' ); ?>:
                </label>
            </td>
            <td>
                <input class="inputbox" type="text" name="emails_per_send" id="emails_per_send" size="180" value="<?php echo $this->newsletter->emails_per_send; ?>" />
            </td>
        </tr>      
        <tr>
            <td width="110" class="key">
                <label for="message">
                    <?php echo JText::_( 'Message' ); ?>:
                </label>
            </td>
            <td>
            <!-- NEWSLETTER SMART TEXT HELP -->
            <a id="toggle" href="#">Toggle Newsletter Smart Text Help</a><br/>
            <div id="smarttexts" style="background-color: #fff; border: 1px solid #D5D5D5; padding: 10px;">
            <table class="stexts">
            <tr><td colspan="2"><strong>Subscriber Smart Text</strong></td><td colspan="2"><strong>URL Smart Text</strong></td></tr>
            <tr><td class="stkey">Subscriber Name: </td><td>{subscriber_name}</td><td class="stkey">One-Click Unsubscribe URL: </td><td>{url:UNSUB}</td></tr>
            <tr><td class="stkey">Subscriber First Name: </td><td>{subscriber_first_name}</td><td class="stkey">Forward to Friend URL: </td><td>{url:FTF}</td></tr>
            <tr><td class="stkey">Subscriber Last Name: </td><td>{subscriber_last_name}</td><td class="stkey">Edit List URL: </td><td>{url:EDITLISTS}</td></tr>
            <tr><td class="stkey">Subscriber Email: </td><td>{subscriber_email}</td><td class="stkey">View Newsletter URL: </td><td>{url:NEWSLETTER}</td></tr>
            <tr><td colspan="2"><strong>Email Open Tracking Image</strong></td><td colspan="2"><strong>Link Smart Text</strong></td></tr>
            <tr><td class="stkey">White 1x1 Image: </td><td>{img:white}</td><td class="stkey">One-Click Unsubscribe Link: </td><td>{link:UNSUB}Your Text here{/link}</td></tr>
            <tr><td class="stkey">Black 1x1 Image: </td><td>{img:black}</td><td class="stkey">Forward to Friend Link: </td><td>{link:FTF}Your Text here{/link}</td></tr>
            <tr><td class="stkey">Red 1x1 Image: </td><td>{img:red}</td><td class="stkey">Edit List Link: </td><td>{link:EDITLISTS}Your Text here{/link}</td></tr>
            <tr><td class="stkey">Green 1x1 Image: </td><td>{img:green}</td><td class="stkey">View Newsletter Link: </td><td>{link:NEWSLETTER}Your Text here{/link}</td></tr>
            <tr><td class="stkey">Blue 1x1 Image: </td><td>{img:blue}</td><td></td><td></td></tr>
            <tr><td colspan="2"><strong>Includes</strong></td><td colspan="2"><strong>Special Smart Text</strong></td></tr>
            <tr><td class="stkey">Joomla! Article: </td><td>{include:article}article_id{/include}</td><td class="stkey">Random Content Display:</td><td>{random:50%}Your Content{/random}</td></tr>
            <tr><td class="stkey">Joomla! Article Title: </td><td>{include:articletitle}article_id{/include}</td><td></td><td></td></tr>
            <tr><td class="stkey">Joomla! Article Intro: </td><td>{include:articleintro}article_id{/include}</td><td></td><td></td></tr>
            <tr><td class="stkey">Joomla! Module: </td><td>{include:module}module_id{/include}</td><td></td><td></td></tr>
            <tr><td class="stkey">Joomla! Module Position: </td><td>{include:moduleposition}position_name{/include}</td><td></td><td></td></tr>
            <tr><td class="stkey">Extra Field Value: </td><td>{include:field}field_id{/include}</td><td></td><td></td></tr>
            </table>
            </div>
            <br/>
            <?php

              if (!($this->isNew)) {
            ?>
            <!-- HTML TEMPLATES -->
            <a id="toggleh" href="#">Toggle HTML Templates</a>
            <br/>
            <div id="htmltemplatesslide" style="border: 1px solid #D5D5D5; background-color: #f9f9f9; padding: 10px; height: 160px !important;">
            <?php
              if (count($templates) > 0) {
                $r = 0;
                foreach ($templates as $template) {
                  if ($template->thumbpath != '') {
                    print '<input type="radio" name="templatetoapply" value="' . $template->id . '"/><img src="' . $template->thumbpath . '" style="border: 1px solid #000;" height="137"/>&nbsp;';
                  }
                  else {
                    print '<input type="radio" name="templatetoapply" value="' . $template->id . '"/> ' . $template->tname . '&nbsp;';
                  }
                  if ($r == 2) {
                    print '<br/><br/>';
                    $r = 0;
                  }
                  $r = $r + 1;
                }
                print '<br/><a href="#" onClick="javascript:useTemplate();">' . JText::_('Apply Template') . '</a>';
              }
              else {
                print JText::_('No templates found') . '.' . '<br/>';
                print '<a href="index.php?option=com_nspro&view=templates">' . JText::_('Create New HTML Templates') . '</a>';
              }

            ?>
            </div>
            <?php
              }
              else {
                print JText::_('Note: If you save, and open for edit again, you will be able to apply HTML Templates.') . '<br/>';
              }
            ?>
            <script type="text/javascript">
            <!--

              var mySlide = new Fx.Slide('smarttexts');

              $('toggle').addEvent('click', function(e){
                e = new Event(e);
                mySlide.toggle();
                e.stop();
              });

              <?php
                if (!($this->isNew)) {
              ?>

              var mySlide2 = new Fx.Slide('htmltemplatesslide');

              $('toggleh').addEvent('click', function(e){
                e = new Event(e);
                mySlide2.toggle();
                e.stop();
              });

              mySlide2.toggle();
              <?php
                }
              ?>

              mySlide.toggle();

            //-->
            </script>
            <br/>
            <?php
              echo $editor->display('message', $this->newsletter->message, '728', '400', '180', '20');
            ?>
            </td>
        </tr>
        <tr>
        </table>
    </fieldset>
</div>
<input type="hidden" name="option" value="com_nspro" />
<input type="hidden" name="id" value="<?php echo $this->newsletter->id; ?>" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="controller" value="nspro" />
</form>
<i>Note: When uploading or deleting attachments, all changes above will be lost!</i>
</td></tr>
<?php
  if (!($this->isNew)) {
    if (count($mattachments) > 0) {
      ?>
<tr><td>
<div class="col width">
<fieldset class="adminform">
<legend>Newsletter Attachments</legend>
<form action="index.php" method="post">
<table>
<?php
  foreach ($mattachments as $mat) {
    print '<tr><td><input type="checkbox" name="attachment[]" value="' . $mat->id . '"/></td><td>'. $mat->filename .'</td></tr>';
  }
?>
</table>
<br/>
<input type="submit" name="deleter" value="Delete Selected Attachments"/>
<input type="hidden" name="option" value="com_nspro" />
<input type="hidden" name="newsletter_id" value="<?php echo $this->newsletter->id; ?>" />
<input type="hidden" name="task" value="deleteattachments" />
<input type="hidden" name="controller" value="nspro" />
</form>
</fieldset>
</div>
</td></tr>
      <?php
    }
?>
<tr><td>
<form enctype="multipart/form-data" action="index.php" method="POST" name="fileattachform">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php print $upfilesize; ?>" />
<div class="col width">
<fieldset class="adminform">
<legend>Add Attachments</legend>
<table class="admintable"><tr>
<td width="110" class="key">Select File:</td>
<td><input id="my_file_element" type="file" name="file_1" size="80">
</td></tr><tr>
<td width="110" class="key">Attached Files (up to 5):</td>
<td><div id="attached_files_list" style="border: 1px solid #CCC; padding: 15px; background: rgb(255, 255, 255) none repeat scroll 0% 0%;"></div>
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="attachallfiles" value="<?php print JText::_('Upload and Attach'); ?>"><br/>
</td></tr>
</table>
<script type="text/javascript"><!--
// Thank you Mister Stickman !!!
// Multiple file selector by Stickman -- http://www.the-stickman.com
// with thanks to: [for Safari fixes] Luis Torrefranca -- http://www.law.pitt.edu and Shawn Parker & John Pennypacker -- http://www.fuzzycoconut.com [for duplicate name bug] 'neal'
function MultiSelector( list_target, max ){this.list_target = list_target;this.count = 0;this.id = 0;if( max ){this.max = max;} else {this.max = -1;};this.addElement = function( element ){if( element.tagName == 'INPUT' && element.type == 'file' ){element.name = 'file_' + this.id++;element.multi_selector = this;element.onchange = function(){var new_element = document.createElement( 'input' );new_element.type = 'file';new_element.setAttribute('size', '80');this.parentNode.insertBefore( new_element, this );this.multi_selector.addElement( new_element );this.multi_selector.addListRow( this );this.style.position = 'absolute';this.style.left = '-1000px';};if( this.max != -1 && this.count >= this.max ){element.disabled = true;};this.count++;this.current_element = element;} else {alert( 'Error: not a file input element' );};};this.addListRow = function( element ){var new_row = document.createElement( 'div' );new_row.setAttribute('style', 'margin-bottom: 10px;');var new_row_button = document.createElement( 'input' );new_row_button.type = 'button';new_row_button.value = 'Delete';new_row.element = element;new_row_button.onclick= function(){this.parentNode.element.parentNode.removeChild( this.parentNode.element );this.parentNode.parentNode.removeChild( this.parentNode );this.parentNode.element.multi_selector.count--;this.parentNode.element.multi_selector.current_element.disabled = false;return false;};new_row.innerHTML = element.value.replace(/^.*\\/, '')+' ';new_row.appendChild( new_row_button );this.list_target.appendChild( new_row );};};
--></script>
<script type="text/javascript"><!--
var multi_selector = new MultiSelector( document.getElementById( 'attached_files_list' ), 5 );
multi_selector.addElement( document.getElementById( 'my_file_element' ) );
--></script>
</fieldset>
</div>

<input type="hidden" name="newsletter_id" value="<?php print $this->newsletter->id; ?>" />
<input type="hidden" name="option" value="com_nspro" />
<input type="hidden" name="task" value="attachfile" />
<input type="hidden" name="boxchecked" value="0" />
<input type="hidden" name="controller" value="nspro" />
</form>
</td></tr>
<?php
  }
  else {
    print '<tr><td><i>Note: Attachments are not available when making a new Newsletter.</i></td></tr>';
  }
?>
</table>
<div class="clr"></div>

<small><i>NS Pro by <a href="http://www.mavrosxristoforos.com/">Mavrosxristoforos.com</a> Copyright 2009-2011</i></small>
<script type="text/javascript"><!--
<?php
$mnLists = explode(',', $this->newsletter->mailing_lists);
if (count($mnLists) > 0) {
  foreach ($mnLists as $mnlist) {
    if ($mnlist != '') {
      print 'if (document.getElementById("list_'.$mnlist.'")) {' . "\n";
      print '  document.getElementById("list_'.$mnlist.'").setAttribute("selected","selected");' . "\n";
      print '}' . "\n";
    }
  }
}
?>
-->
</script>